package com.example.yangyinzhaixian;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private int[] image = new int[]{R.drawable.new_feature_pic1, R.drawable.new_feature_pic2, R.drawable.new_feature_pic3, R.drawable.new_feature_pic4};
    private ViewPager feature_main_viewpager;
    private ArrayList<ImageView> data;
    private Button feature_main_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        data=new ArrayList<>();
        for (int i = 0; i < image.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(GridView.LayoutParams.MATCH_PARENT,GridView.LayoutParams.MATCH_PARENT));
            imageView.setImageResource(image[i]);
            data.add(imageView);
        }
        Myade myade = new Myade(data);
        feature_main_viewpager.setAdapter(myade);
        feature_main_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==3)
                {
                    feature_main_btn.setVisibility(View.VISIBLE);
                }
                else {
                    feature_main_btn.setVisibility(View.GONE);
            }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        feature_main_viewpager = (ViewPager) findViewById(R.id.feature_main_viewpager);
        feature_main_btn = (Button) findViewById(R.id.feature_main_btn);
        feature_main_btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.feature_main_btn:
                Intent intent = new Intent(this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
    class Myade extends PagerAdapter{
        private ArrayList<ImageView> data;

        public Myade(ArrayList<ImageView> data) {
            this.data = data;
        }
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(data.get(position));
            return data.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(data.get(position));
        }
    }
}
